Imports System.Data
Imports System.Data.SqlClient

Public Class frmConsultaExistencias
   Inherits System.Windows.Forms.Form

   Private mdsExistencias As DataSet

#Region " Windows Form Designer generated code "

   Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If Not (components Is Nothing) Then
            components.Dispose()
         End If
      End If
      MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
   Friend WithEvents editProducto As System.Windows.Forms.TextBox
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents editDescripcion As System.Windows.Forms.TextBox
   Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
   Friend WithEvents dgExistencias As System.Windows.Forms.DataGrid
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.GroupBox1 = New System.Windows.Forms.GroupBox
      Me.editDescripcion = New System.Windows.Forms.TextBox
      Me.Label1 = New System.Windows.Forms.Label
      Me.editProducto = New System.Windows.Forms.TextBox
      Me.GroupBox2 = New System.Windows.Forms.GroupBox
      Me.dgExistencias = New System.Windows.Forms.DataGrid
      Me.GroupBox1.SuspendLayout()
      Me.GroupBox2.SuspendLayout()
      CType(Me.dgExistencias, System.ComponentModel.ISupportInitialize).BeginInit()
      Me.SuspendLayout()
      '
      'GroupBox1
      '
      Me.GroupBox1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                  Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
      Me.GroupBox1.Controls.Add(Me.editDescripcion)
      Me.GroupBox1.Controls.Add(Me.Label1)
      Me.GroupBox1.Controls.Add(Me.editProducto)
      Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
      Me.GroupBox1.Name = "GroupBox1"
      Me.GroupBox1.Size = New System.Drawing.Size(544, 48)
      Me.GroupBox1.TabIndex = 0
      Me.GroupBox1.TabStop = False
      '
      'editDescripcion
      '
      Me.editDescripcion.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                  Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
      Me.editDescripcion.Location = New System.Drawing.Point(200, 16)
      Me.editDescripcion.Name = "editDescripcion"
      Me.editDescripcion.ReadOnly = True
      Me.editDescripcion.Size = New System.Drawing.Size(336, 20)
      Me.editDescripcion.TabIndex = 2
      Me.editDescripcion.TabStop = False
      Me.editDescripcion.Text = ""
      '
      'Label1
      '
      Me.Label1.Location = New System.Drawing.Point(8, 24)
      Me.Label1.Name = "Label1"
      Me.Label1.Size = New System.Drawing.Size(56, 16)
      Me.Label1.TabIndex = 1
      Me.Label1.Text = "Producto"
      '
      'editProducto
      '
      Me.editProducto.Location = New System.Drawing.Point(64, 16)
      Me.editProducto.Name = "editProducto"
      Me.editProducto.Size = New System.Drawing.Size(128, 20)
      Me.editProducto.TabIndex = 0
      Me.editProducto.Text = ""
      '
      'GroupBox2
      '
      Me.GroupBox2.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                  Or System.Windows.Forms.AnchorStyles.Left) _
                  Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
      Me.GroupBox2.Controls.Add(Me.dgExistencias)
      Me.GroupBox2.Location = New System.Drawing.Point(8, 64)
      Me.GroupBox2.Name = "GroupBox2"
      Me.GroupBox2.Size = New System.Drawing.Size(544, 240)
      Me.GroupBox2.TabIndex = 1
      Me.GroupBox2.TabStop = False
      '
      'dgExistencias
      '
      Me.dgExistencias.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                  Or System.Windows.Forms.AnchorStyles.Left) _
                  Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
      Me.dgExistencias.CaptionVisible = False
      Me.dgExistencias.DataMember = ""
      Me.dgExistencias.HeaderForeColor = System.Drawing.SystemColors.ControlText
      Me.dgExistencias.Location = New System.Drawing.Point(8, 16)
      Me.dgExistencias.Name = "dgExistencias"
      Me.dgExistencias.ReadOnly = True
      Me.dgExistencias.Size = New System.Drawing.Size(528, 216)
      Me.dgExistencias.TabIndex = 0
      Me.dgExistencias.TabStop = False
      '
      'frmConsultaExistencias
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(560, 309)
      Me.Controls.Add(Me.GroupBox2)
      Me.Controls.Add(Me.GroupBox1)
      Me.Name = "frmConsultaExistencias"
      Me.Text = "Consulta de Existencias"
      Me.GroupBox1.ResumeLayout(False)
      Me.GroupBox2.ResumeLayout(False)
      CType(Me.dgExistencias, System.ComponentModel.ISupportInitialize).EndInit()
      Me.ResumeLayout(False)

   End Sub

#End Region

   Private Sub editProducto_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles editProducto.KeyUp

      If e.KeyCode = Keys.F3 Then

         Dim strResult As String
         Dim fBusqueda As New frmBusqueda

         fBusqueda.Tabla = "Inventory"
         fBusqueda.Campos = "InvtID, Descr"

         fBusqueda.ShowDialog()
         strResult = fBusqueda.Valor

         If strResult <> "" Then
            editProducto.Text = strResult
            editDescripcion.Focus()
         End If

      End If

   End Sub

   Private Sub editProducto_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles editProducto.Validating

      If editProducto.Text.Trim <> "" Then
         Try

            Cursor.Current = Cursors.WaitCursor

            editDescripcion.Text = BuscarStrDato("Inventory", "Descr", "InvtId = '" & editProducto.Text.Trim & "'")
            BuscarExistencias(editProducto.Text.Trim)

         Catch ex As Exception
         Finally
            Cursor.Current = Cursors.Default
         End Try
      End If

   End Sub

   Private Sub BuscarExistencias(ByVal strProducto As String)
      Dim cnnConnection As New SqlConnection

      Try

         cnnConnection.ConnectionString = gstrConnection
         cnnConnection.Open()

         Dim scmdSelect As New SqlCommand
         scmdSelect.CommandText = "Select Sucursal, Almacen, Localizacion, Producto, Descripcion, " & _
                                  "       Existencia, Requerida, Embarcada, Apartada,  " & _
                                  "      (Existencia + Requerida + Embarcada - Apartada) As Disponibilidad " & _
                                  "From pvvwExistencias " & _
                                  "Where Empresa = @Empresa " & _
                                  "  And Regional = @Regional " & _
                                  "  And Producto = @Producto "

         scmdSelect.CommandType = CommandType.Text
         scmdSelect.Connection = cnnConnection

         scmdSelect.Parameters.Add("@Empresa", gstrEmpresa)
         scmdSelect.Parameters.Add("@Regional", gstrRegional)
         scmdSelect.Parameters.Add("@Producto", strProducto)

         If mdsExistencias.Tables.Count() > 0 Then
            mdsExistencias.Tables("Existencias").Clear()
         End If

         Dim daExistencias As New SqlDataAdapter
         daExistencias.SelectCommand = scmdSelect

         daExistencias.Fill(mdsExistencias, "Existencias")

      Catch eOpen As SqlException

         System.Windows.Forms.MessageBox.Show(eOpen.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Stop)

      Catch eGeneral As Exception

         System.Windows.Forms.MessageBox.Show(eGeneral.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Stop)

      Finally

         If cnnConnection.State = ConnectionState.Open Then
            cnnConnection.Close()
         End If

      End Try

   End Sub

   Private Sub frmConsultaExistencias_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      mdsExistencias = New DataSet
      BuscarExistencias("")

      dgExistencias.DataSource = mdsExistencias.Tables("Existencias")

   End Sub

End Class
